import { MessageBox } from 'element-ui'
import { create } from 'vue-modal-dialogs'

const components = {
  SysConfigDialog: () => import('@/views/editor/dialogs/sys-config-dialog'),
  GraphOpenDialog: () => import('@/views/editor/dialogs/graph-open-dialog'),
  GraphExportDialog: () => import('@/views/editor/dialogs/graph-export-dialog'),
  GraphImportDialog: () => import('@/views/editor/dialogs/graph-import-dialog'),
  LayoutConfigDialog: () =>
    import('@/views/editor/dialogs/layout-config-dialog'),
  LinkManagerDialog: () => import('@/views/editor/dialogs/link-manager-dialog')
}

// 记录已打开的对话框
const state = {}

function Show(name, args = {}) {
  if (!components.hasOwnProperty(name)) {
    MessageBox.alert('模态对话框' + name + '尚未开发', '提示', {
      confirmButtonText: '确定',
      customClass: 'message-dailog',
      showClose: false
    })
    return Promise.reject('模态对话框' + name + '尚未开发')
  }

  if (state.hasOwnProperty(name)) {
    MessageBox.alert('模态对话框' + name + '尚未关闭', '提示', {
      confirmButtonText: '确定',
      customClass: 'message-dailog',
      showClose: false
    })
    return Promise.reject('模态对话框' + name + '尚未关闭')
  }

  // 加载模块
  const comp = components[name]()
  // 创建对话框对象
  const dialogFunction = create(comp)
  // 启动对话框(dialog就是一个promise)
  return new Promise((resolve, reject) => {
    // 记录对话框
    state[name] = true
    // 显示对话框
    dialogFunction(args)
      .then(res => {
        console.log('[正常]删除对话框记录', state, name)
        // 删除对话框记录
        delete state[name]
        resolve(res)
      })
      .catch(err => {
        console.log('[异常]删除对话框记录', state, name)
        // 删除对话框记录
        delete state[name]
        reject(err)
      })
  })
}

// function Close(name) {}

export default { Show }
